package com.sptpc.stu.controller;

import cn.hutool.core.util.StrUtil;
import com.sptpc.stu.common.controller.BaseController;
import com.sptpc.stu.common.response.JSONResponse;
import com.sptpc.stu.common.utils.QueryPage;
import com.sptpc.stu.controller.request.*;
import com.sptpc.stu.model.my.User;
import com.sptpc.stu.repository.my.UserRepository;
import com.sptpc.stu.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/account")
public class AccountController extends BaseController {


//    @Autowired
//    private AnoMailServiceImpl anoMailService;


    @Autowired
    private MailService mailService;


    @Autowired
    private UserRepository userRepository;


    // 1、查询接口
//    @GetMapping("/list")
//    public JSONResponse getAllAccounts(SearchUsrRequest usrRequest,
//                                       QueryPage queryPage){
//
//        Page<User> page = null;
//        if(StrUtil.isNotBlank(usrRequest.getName())) {
//            String searchName = "%" + usrRequest.getName() + "%";
//            if (usrRequest.getGender()==null) {
//                page = userRepository.findStudentsPageByNameLike(
//                        searchName,
//                        PageRequest.of(queryPage.getPage() - 1, queryPage.getLimit())
//                );
//            } else {
//                page = userRepository.findStudentsPageByNameLikeAndGender(
//                        searchName,
//                        usrRequest.getGender(),
//                        PageRequest.of(queryPage.getPage() - 1, queryPage.getLimit())
//                );
//            }
//        } else {
//            if (usrRequest.getGender()!=null) {
//                page = userRepository.findStudentsPageByGender(
//                        usrRequest.getGender(),
//                        PageRequest.of(queryPage.getPage() - 1, queryPage.getLimit())
//                );
//            } else {
//                page = userRepository.findAll(
//                        PageRequest.of(queryPage.getPage() - 1, queryPage.getLimit()));
//            }
//        }
//
//        return JSONResponse.ofSuccess(getData(page));
//    }


    // 发送验证码到邮箱，邮箱参数直接跟在Url后面，etc.http://localhost:8080/account/sendCAPTCHA?email=737461472@qq.com

    @GetMapping("/sendCAPTCHA")
    @ResponseBody
    public JSONResponse sendEmail(String email, HttpSession httpSession){
        return mailService.sendCodeMail(email, httpSession);
    }

    //注册接口，需要对验证码进行验证

    /**
     前端发送请求示例：
     {
     "accountPhone":"19964789",
     "email":"737461472@qq.com",
     "checkCode":"266669",
     "password":"iloveu"
     }
     */

    @PostMapping(value = "/regist",
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public JSONResponse regist(@RequestBody AddNewUserRequest userVo){
        return mailService.registeredOrUpdate(userVo);
    }



    //登录接口，前端使用JSON
    /**
     * 示例
     {
     "accountPhone":"19964789",
     "password":"iloveu"
     }
     */
    @PostMapping("/login")
    @ResponseBody
    public JSONResponse login(@RequestBody SearchaAccRequest accRequest){

        return mailService.loginIn(accRequest);
    }

    @PostMapping(value = "/checkCode")
    private JSONResponse tocheckCodeRight(@RequestBody AddNewUserRequest addNewUserRequest){
        return mailService.checkCode(addNewUserRequest);
    }

}
